package com.shengdong.water.feature.signIn

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.facade.annotation.Route
import com.kongzue.dialogx.dialogs.WaitDialog
import com.shengdong.water.R
import com.shengdong.water.code.adapter.SignRecordAdapter
import com.shengdong.water.code.base.BaseRefreshActivity
import com.shengdong.water.code.bean.SignInBean
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.config.UserConfig
import com.shengdong.water.code.router.RouterManager
import com.shengdong.water.code.viewmodel.PageListViewModel
import com.shengdong.water.databinding.ActivitySignRecordBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = RouterConfig.SignIn.SIGN_RECORD_PATH)
@AndroidEntryPoint
class SignRecordsActivity : BaseRefreshActivity<SignInBean,ActivitySignRecordBinding,PageListViewModel>() {


    override fun getRecyclerView() = binding.recyclerRecord

    override fun getSmartRefreshLayout() = binding.refresh

    override fun getViewModelClass() = PageListViewModel::class.java

    override fun getAdapter() = SignRecordAdapter()


    override fun getLayoutId() = R.layout.activity_sign_record


    override fun showLoading(loading: String) {
        WaitDialog.show(loading)
    }

    override fun dismissLoading() {
        super.dismissLoading()
        WaitDialog.dismiss()
    }

    override fun initViews() {
        super.initViews()
        setTitle("签到记录")
        setupListener()
    }

    private fun setupListener() {

        baseRecyclerAdapter.setOnItemClickListener { signInBean, i ->
            RouterManager.build(RouterConfig.SignIn.RECORD_DETAIL_PATH)
                .withString("id", signInBean.id)
                .navigate()
        }

    }

    override fun initData() {
        super.initData()
        loadData()
    }

    override fun loadData() {
        val signMap: MutableMap<String, Any> = mutableMapOf()
        signMap["createUser"] = UserConfig.user?.userId?:1
        viewModel.getSignList(signMap,currentPage,pageSize)
    }

    override fun observeData() {
        super.observeData()
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.signList.collect { pageData ->
                        pageData.let {
                            if(currentPage == 1 && it.isEmpty()){
                                binding.recyclerRecord.setEmptyView(binding.layoutEmpty.root)
                                return@collect
                            }

                            if (it.isEmpty()) {
                                finishRefreshAndLoadMore(true, currentPage)
                                return@collect

                            }
                            handleData(it)
                        }
                    }
                }



            }
        }
    }


}